package org.zgy.stock.threads;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.zgy.stock.model.CompanyInfo;
import org.zgy.stock.service.CompanyInfoService;
import org.zgy.stock.utils.okhttp3.OkHttpUtil;

public class CompanyInfoThead implements Runnable {
    private final static String API_URL = "https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_BASIC_ORGINFO&columns=ALL&quoteColumns=&filter=(SECUCODE%3D%22STOCK_CODE_WITH_MARKET%22)&pageNumber=1&pageSize=1&sortTypes=&sortColumns=&source=HSF10&client=PC&v=04556322507711166";
    private final CompanyInfoService companyInfoService;
    private final String code;
    public CompanyInfoThead(String code, CompanyInfoService companyInfoService) {
        this.code = code;
        this.companyInfoService = companyInfoService;
    }
    @Override
    public void run() {
        String stock_code = "";
        if (code.startsWith("6")){
            stock_code = code + ".SH";
        }else {
            stock_code = code + ".SZ";
        }
        String result = OkHttpUtil.getRequest(API_URL.replace("STOCK_CODE_WITH_MARKET", stock_code), null);
        DfcfResponse<CompanyInfo> response = JSON.parseObject(
                result,
                new TypeReference<DfcfResponse<CompanyInfo>>() {}
        );

        if (response.isSuccess()) {
            for (CompanyInfo company : response.getResult().getData()) {
                System.out.println(company);
//                companyInfoService.insert(company);
            }
        } else {
            System.err.println("请求失败: " + response.getMessage());
        }
    }

}


